package com.gtgj.widget.util;

import ohos.app.Context;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.net.NetHandle;
import ohos.net.NetManager;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtils {

    public static void reqDate(Context context, Callback callback){
        context.getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(new Runnable() {
            @Override
            public void run() {
                boolean notify = false;
                NetManager netManager = NetManager.getInstance(context);
                if (!netManager.hasDefaultNet()) {
                    if (callback != null) {
                        callback.call(false, "");
                    }
                    return;
                }
                NetHandle netHandle = netManager.getDefaultNet();
                HttpURLConnection connection = null;
                try {
                    String urlString = "https://train.rsscc.com/trainwap/cfg/remindConfig/grabCalendar"; // 开发者根据实际情况自定义EXAMPLE_URL
                    URL url = new URL(urlString);
                    URLConnection urlConnection = netHandle.openConnection(url,
                            java.net.Proxy.NO_PROXY);
                    if (urlConnection instanceof HttpURLConnection) {
                        connection = (HttpURLConnection) urlConnection;
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(5000);
                        connection.connect();
                        if (connection.getResponseCode() == 200) {
                            InputStream inputStream = connection.getInputStream();
                            String content = TypeUtils.convertInputStreamToString(inputStream);
                            if (callback != null) {
                                notify = true;
                                callback.call(true, content);
                            }
                        }
                    }
                } catch(Exception e) {
                } finally {
                    if (!notify && callback != null) {
                        callback.call(false, "");
                    }
                    if (connection != null){
                        connection.disconnect();
                    }
                }
            }
        });
    }

    public static void reqTrip(Context context, Callback callback){
        context.getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(new Runnable() {
            @Override
            public void run() {
                boolean notify = false;
                NetManager netManager = NetManager.getInstance(context);
                if (!netManager.hasDefaultNet()) {
                    if (callback != null) {
                        callback.call(false, "");
                    }
                    return;
                }
                NetHandle netHandle = netManager.getDefaultNet();
                HttpURLConnection connection = null;
                try {
                    String urlString = "https://train.rsscc.com/trainwap/cfg/remindConfig/grabCalendar"; // 开发者根据实际情况自定义EXAMPLE_URL
                    URL url = new URL(urlString);
                    URLConnection urlConnection = netHandle.openConnection(url,
                            java.net.Proxy.NO_PROXY);
                    if (urlConnection instanceof HttpURLConnection) {
                        connection = (HttpURLConnection) urlConnection;
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(5000);
                        connection.connect();
                        if (connection.getResponseCode() == 200) {
                            InputStream inputStream = connection.getInputStream();
                            String content = TypeUtils.convertInputStreamToString(inputStream);
                            if (callback != null) {
                                notify = true;
                                callback.call(true, content);
                            }
                        }
                    }
                } catch(Exception e) {
                } finally {
                    if (!notify && callback != null) {
                        callback.call(false, "");
                    }
                    if (connection != null){
                        connection.disconnect();
                    }
                }
            }
        });
    }

    public interface Callback{
        void call(boolean success, String content);
    }
}
